# coding=utf-8
from flask import Flask, request
from util import image_to_base64, base64_to_image
from flask_cors import *
import json
import os
import darknet

project_root_path = os.path.join(os.path.dirname(__file__), '../')
app = Flask(__name__)
CORS(app)

@app.route('/cv/v1/obj_detect', methods=['GET', 'POST'])
def obj_detect():
    img_base64 = request.json['img_base64']
    img_PIL = base64_to_image(img_base64)
    # 检测
    img_PIL.save("image.png")

    img_detected = darknet.performDetect(imagePath="image.png")
    # 返回内容，返回json
    jsonresult = {}
    jsonresult['base64'] = image_to_base64(img_detected)
    dataPModel = {"msg": "object detect success", "code": 200, "result": jsonresult}
    return json.dumps(dataPModel)

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8520, use_reloader=True, debug=True)
